<?php
/**
 * Created by PhpStorm.
 * User: qttx
 * Date: 2019/5/30
 * Time: 15:07
 */
namespace app\admin\model;


use think\Db;

class UserModel extends BaseModel
{
    protected $table = "xst_user";
    protected $rule = [
        'id' => '',
    ];
    protected $msg = [
        'id' => '',
    ];
    /**
     * 首页
     */
    public function index($condition = 1)
    {    
      if(input('get.sub')){
            $phone = input('phone');
            if(!empty($phone)){
                $condition .= "  and (phone like '%".$phone."%')";
            }
      }
        $res = $this->field('u.*,b.circulate,b.circulation')->alias('u')->join('xst_balance b','b.uid = u.id','left')->where($condition)->order('createtime desc')->paginate(10,false,['query'=>request()->param()]);
        return $res;
    }
    /**
     * 用户状态修改
     * $type 1正常，给用户禁用 0禁用，给用户恢复
     */
    public function user_status($id,$type)
    {
        $res = $this->where('id',$id)->update(['status'=>$type]);
        return $res;
    }
    /**
     * 获取单条信息
    */
    public function getOneId($id)
    {
        return $this->where('id',$id)->find();
    }
    /**
     * 修改用户信息
    */
    public function edit()
    {
        $nickname = request()->post('nickname');
        $mail = request()->post('mail');
        $phone = request()->post('phone');
        $star = request()->post('star');
        $type = request()->post('type');
        $status = request()->post('status');
        $id = request()->post('id');
        $data = [
            'nickname'=>$nickname,
            'mail'=>$mail,
            'phone'=>$phone,
            'star'=>$star,
            'type'=>$type,
            'status'=>$status,
        ];
        $res = $this->where('id',$id)->update($data);
        return $res;
    }
    public function del()
    {
        $id = request()->post('id');
        Db::startTrans();
        $res = $this->where('id',$id)->delete();
        $ress = Db::name('balance')->where('id',$id)->delete();
        if($res && $ress)
        {
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }
    public function dell()
    {
        $id = request()->post('id');
        $res = $this->where('id','in',$id)->delete();
        $ress = Db::name('xst_balance')->where('id','in',$id)->delete();
        if($res && $ress)
        {
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }
}